package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.BaiMiaoServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "baimiao")
public class BaiMiaoAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(BaiMiaoAdaptor.class);

	public void order() {
		
	    OrderBean orderBean = OrderInfoContext.getContext();
		String pkgNo = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String tradeNo = orderBean.getIds();
		String callbackURL = BetweenProperties.CALLBACK_URL+"Baimiao";
		orderBean.setSendTime(new Date());
		String result = BaiMiaoServiceImpl.service.order(pkgNo,phoneNo,tradeNo,callbackURL);
		log.info("百秒訂購請求返回結果 order()："+result+",该订单号为："+tradeNo);
		
		JSONObject resjson = JSONObject.fromObject(result);

	    String resultcode =  resjson.getString("resultcode");//状态码

		if ("00000".equals(resultcode)) {
			orderBean.setStatus("11");
			orderBean.setMessage(resjson.getString("resultdescription"));
			orderBean.setErrorMsg(resjson.getString("resultdescription"));
			orderBean.setSuppOrderNo(resjson.getString("userorderno"));
			
		}else {
			orderBean.setStatus("8");
			orderBean.setMessage(resjson.getString("resultdescription"));
			orderBean.setErrorMsg(resjson.getString("resultdescription"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();

        String suppOrderNo = orderBean.getSuppOrderNo();
        if ("".equals(suppOrderNo)) {
        	orderBean.setMessage("该订单业务流水为空");
			orderBean.setErrorMsg("受理中，无订单");
			orderBean.setStatus("");
		}else {
			String result = BaiMiaoServiceImpl.service.orderStatus(suppOrderNo);
			log.info("between- 百妙骏业BaiMiaoService.orderStatus() 查询响应response<> :" + result);
		
			JSONObject resjson = JSONObject.fromObject(result);

		    String resultcode = resjson.getString("resultcode");//状态码

			if ("0".equals(String.valueOf(resultcode))) {
				orderBean.setStatus("7");
	        	orderBean.setErrorMsg("充值成功");
	        	orderBean.setMessage("充值成功");
				
							
			} else if ("1".equals(resultcode)) {
				orderBean.setStatus("8");
	        	orderBean.setErrorMsg(resjson.getString("resultdescription"));
	        	orderBean.setMessage(resjson.getString("resultdescription"));
			
			}else {
				orderBean.setStatus("1");
	        	orderBean.setErrorMsg(resjson.getString("resultdescription"));
	        	orderBean.setMessage(resjson.getString("resultdescription"));

			}
		}
		
	
		BsmOrderResultExecutor.service.setOrder(orderBean);
		
	}
	

}
